<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- card部分 -->
    <el-card>
      <!-- 搜索框以及按钮部分 -->
      <el-row :gutter="20" class="search">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryobj.query" clearable @clear="clearfn">
            <el-button slot="append" icon="el-icon-search" @click="searchContent"></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <router-link :to="{ path: 'goods/add' }">
            <el-button type="primary"> 添加商品 </el-button>
          </router-link>
        </el-col>
      </el-row>
      <!-- 商品table部分 -->
      <el-table :data="shopdata" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
          width="716"
        ></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          width="93.4"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="68.6"
        ></el-table-column>
        <el-table-column label="创建时间" width="160">
          <template slot-scope="scope">
            {{ scope.row.add_time | getdate }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="primary"
              icon="el-icon-delete"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryobj.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryobj.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalNum"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inputvalue: "",
      shopdata: [],
      queryobj: {
        query: "",
        pagenum: 1, //当前的页码
        pagesize: 5, //每页显示的条数
      },
      totalNum: 0,
    };
  },
  created() {
    this.getGoods();
  },
  methods: {
    async getGoods() {
      let { data: res } = await this.$axios.get(
        "http://timemeetyou.com:8889/api/private/v1/goods",
        {
          params: this.queryobj,
        }
      );
      if (res.meta.status === 200) {
        this.$message.success("获取商品列表数据成功!");
      }
    //   console.log(res);
      this.shopdata = res.data.goods;
      this.totalNum = res.data.total;
    },
    handleSizeChange(pagesize) {
      this.queryobj.pagesize = pagesize;
      this.getGoods();
    },
    handleCurrentChange(pagenum) {
      this.queryobj.pagenum = pagenum;
      this.getGoods();
    },
    searchContent(){
        this.getGoods();
    },
    clearfn(){
        this.getGoods();
    }
  },
};
</script>

<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 15px;
}
.search {
  margin-bottom: 15px;
}
.el-table {
  margin-bottom: 15px;
}
</style>